package com.edt.ichannel.utils;

import cc.edt.iceutils2.string.IceStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 
* 
*----------Dragon be here!----------/ 
* 　　　┏┓　　　┏┓ 
* 　　┏┛┻━━━┛┻┓ 
* 　　┃　　　　　　　┃ 
* 　　┃　　　━　　　┃ 
* 　　┃　┳┛　┗┳　┃ 
* 　　┃　　　　　　　┃ 
* 　　┃　　　┻　　　┃ 
* 　　┃　　　　　　　┃ 
* 　　┗━┓　　　┏━┛ 
* 　　　　┃　　　┃神兽保佑 
* 　　　　┃　　　┃代码无BUG！ 
* 　　　　┃　　　┗━━━┓ 
* 　　　　┃　　　　　　　┣┓ 
* 　　　　┃　　　　　　　┏┛ 
* 　　　　┗┓┓┏━┳┓┏┛ 
* 　　　　　┃┫┫　┃┫┫ 
* 　　　　　┗┻┛　┗┻┛ 
* ━━━━━━神兽出没━━━━━━
 */

/**
 * 文件操作
 * 
 * @author sl
 *
 */
public class FileUtil {
	private static Logger  logger= LogManager.getLogger(FileUtil.class);

	public static Set<String> readFileContent(String path, String fileName, String charSet) throws IOException {
		Set<String> contents = new LinkedHashSet<>();
		File file = null;
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader bufferedReader = null;
		try {

			file = new File(path + fileName);
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, charSet);
			bufferedReader = new BufferedReader(isr);

			String content = null;
			while ((content = bufferedReader.readLine()) != null) {
				if(IceStringUtils.isNoneBlank(content)){
					contents.add(content);
				}
			}
			return contents;
		} catch (IOException e) {
			return null;
		} finally {

			try {
				if (fis != null) {
					fis.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static boolean copyFile(String resourcePath, String descPath, String fileName) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			// 1、被复制的源文件
			fis = new FileInputStream(resourcePath + fileName);
			// 2、复制之后产生的新的文件
			File toFile = new File(descPath);
			if (!toFile.exists()) {
				toFile.mkdirs();
			}
			fos = new FileOutputStream(descPath + fileName);
			// 3、自定义缓冲区对象
			byte[] buf = new byte[1024];
			int by = 0;
			while ((by = fis.read(buf)) != -1) {
				fos.write(buf, 0, by);
			}
			return true;

		} catch (Exception e) {
			e.printStackTrace();
			return false;

		}finally{
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(fos != null){
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public static String getFielName(String fileName, int day,String subfix) {
		SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
		Date beginDate = new Date();
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		date.set(Calendar.DATE, date.get(Calendar.DATE) - day);
		if(fileName != null && !"null".equals(fileName)){
			return fileName + dft.format(date.getTime()) + subfix;
		}else{
			return dft.format(date.getTime()) + subfix;
		}
	}

	public static Date strToDateLong(String strDate) {
		String dataStr = "2014/01/21 20:46:20";
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(dataStr, pos);
		return strtodate;
	}
	
	
	public static String readFileContentAsStr(String path, String fileName, String charSet) {
		StringBuffer contents = new StringBuffer();
		File file = null;
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader bufferedReader = null;
		try {

			file = new File(path + fileName);
			fis = new FileInputStream(file);
			isr = new InputStreamReader(fis, charSet);
			bufferedReader = new BufferedReader(isr);

			String content = null;
			while ((content = bufferedReader.readLine()) != null) {
				if(!"".equals(content)){
					contents.append(content);
				}
			}
			return contents.toString();
		} catch (IOException e) {
			return null;
		} finally {

			try {
				if (fis != null) {
					fis.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public  static boolean write(String fileName, String fileContext, String writeTempFielPath) {
		try {
			logger.info("写文件："+writeTempFielPath + "/" + fileName);
			File f = new File(writeTempFielPath + "/" + fileName);
			if (!f.exists() && !f.createNewFile()) {
				logger.info("文件不存在，创建失败!");
			}

			BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
			bw.write(fileContext.replaceAll("\n", "\r\n"));
			bw.flush();
			bw.close();
			return true;
		} catch (Exception var5) {
			logger.error("写文件没有成功");
			var5.printStackTrace();
			return false;
		}
	}

	public  static boolean delete(File f) {
		if(f.exists()){
			return f.delete();
		}
		return false;
	}


	public static List<String> readFileContentList(File downLoadFile,String charSet) {
		List<String> contents = new ArrayList<>();
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader bufferedReader = null;
		try {

			fis = new FileInputStream(downLoadFile);
			isr = new InputStreamReader(fis, charSet);
			bufferedReader = new BufferedReader(isr);
			String content;
			while ((content = bufferedReader.readLine()) != null) {
				if(IceStringUtils.isNoneBlank(content)){
					contents.add(content);
				}
			}
			return contents;
		} catch (IOException e) {
			return null;
		} finally {

			try {
				if (fis != null) {
					fis.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
